String Methods

Besides the primary string functions we've talked about, there are a few others you should be aware of.

isXXXX

The isxxxx() methods help perform checks on strings.

isalnum() checks if a string is alphanumeric


In [4]:
s = "turtle"

In [5]:
s.isalnum()


Out[5]:
True

isalnum() checks if a string is alphabetic


In [6]:
s.isalpha()


Out[6]:
True

In [7]:
y = "turtle3"
y.isalpha()


Out[7]:
False

islower() checks if string characters are lower


In [8]:
y.islower()


Out[8]:
True

isupper() for uppercase


In [14]:
y.isupper()

"Wow".isupper()

"WOW".isupper()


Out[14]:
True

isspace() returns True if a string is all whitespace


In [11]:
" ".isspace()


Out[11]:
True

split() and partition()


In [15]:
"hello".partition("l")


Out[15]:
('he', 'l', 'lo')

In [17]:
"hello".split()


Out[17]:
['hello']

In [19]:
"hello".split("l")


Out[19]:
['he', '', 'o']